      1  /**

      2     A triangular shape composed of stacked unit squares like this:

      3     []

      4     [][]

      5     [][][]

      6     . . .

      7  */

      8  public class Triangle

      9  {

     10     private int width;

     11

     12     /**

     13        Constructs a triangular shape.

     14        @param aWidth the width (and height) of the triangle

     15     */

     16     public Triangle(int aWidth)

     17     {

     18        width = aWidth;

     19     }

     20

     21     /**

     22        Computes the area of the triangle.

     23        @return the area

     24     */

     25     public int getArea()

     26     {

     27        if (width <= 0) { return 0; }

     28        else if (width == 1) { return 1; }

     29        else

     30        {

     31           Triangle smallerTriangle = new Triangle(width - 1);

     32           int smallerArea = smallerTriangle.getArea();

     33           return smallerArea + width;

     34        }

     35     }

     36  }